<?php

namespace api\modules\v1\base;

use api\modules\commons\Func;
use Yii;
use yii\base\ErrorException;
use yii\base\Model;
use yii\base\UserException;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
use yii\rest\Controller;
use yii\base\InvalidConfigException;
use yii\filters\auth\HttpBasicAuth;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\Response;

class RestApiBaseController extends Controller
{
    // 操作成功标示码
    const ACTION_SUCCESS_CODE = 0;

    // 操作失败标示码
    const ACTION_FAIL_CODE = 1;

    /**
     * 开发的权限
     * TODO 【不校验access_token】
     * @var array
     */
    private $allowedApis = [
        'user/login',
        'user/register',
    ];


    /**
     * 关闭csrf验证
     * @var bool
     */
    public $enableCsrfValidation = false;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        Yii::$app->getResponse()->on(Response::EVENT_BEFORE_SEND, [$this, 'beforeSend']);
    }

    /**
     * 验证授权
     * @return array|array[]
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            //这边选择你要开启的验证方式
            'authMethods' => [
                /*HttpBasicAuth::className(),*/
                HttpBearerAuth::className(),
                /*QueryParamAuth::className(),*/
            ],

        ];

        return $behaviors;
    }

    /**
     * 重写
     *
     * @param $action
     *
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction( $action )
    {
        if ( $this->isAllowedApi() ) {
            return true;
        }
        parent::beforeAction( $action );
        return true;
    }

    public function afterAction($action, $result)
    {
        // 格式化响应 JSON
        Yii::$app->response->format = Response::FORMAT_JSON;

        // 重置登录过期时间
        /*if ( !$this->isAllowedApi() ) {
            Yii::$app->user->identity->resetAccessTokenExpirer();
        }*/

        return parent::afterAction($action, $result);
    }

    /**
     * 注销自带的 rest
     * @return array
     */
    public function actions()
    {
        $actions = parent::actions();
        unset(
            $actions[ 'index' ] ,
            $actions[ 'update' ] ,
            $actions[ 'create' ] ,
            $actions[ 'delete' ]
        );
        return $actions;
    }

    /**
     * 验证是否是开发的接口请求
     * @return bool
     */
    private function isAllowedApi()
    {
        $controllerId = Yii::$app->controller->id;
        $action = Yii::$app->controller->action->id;
        $requestApi = $controllerId . '/' . $action;
        return in_array( $requestApi , $this->allowedApis );
    }

    /**
     * 更改数据输出格式
     * 默认情况下输出Json数据
     * 如果客户端请求时有传递$_GET['callback']参数，输入Jsonp格式
     * 请求正确时数据为  {"success":true,"data":{...}}
     * 请求错误时数据为  {"success":false,"data":{"name":"Not Found","message":"页面未找到。","code":0,"status":404}}
     *
     * 200: OK。一切正常。
     * 201: 响应 POST 请求时成功创建一个资源。Location header 包含的URL指向新创建的资源。
     * 204: 该请求被成功处理，响应不包含正文内容 (类似 DELETE 请求)。
     * 304: 资源没有被修改。可以使用缓存的版本。
     * 400: 错误的请求。可能通过用户方面的多种原因引起的，例如在请求体内有无效的JSON 数据，无效的操作参数，等等。
     * 401: 验证失败。
     * 403: 已经经过身份验证的用户不允许访问指定的 API 末端。
     * 404: 所请求的资源不存在。
     * 405: 不被允许的方法。 请检查 Allow header 允许的HTTP方法。
     * 415: 不支持的媒体类型。 所请求的内容类型或版本号是无效的。
     * 422: 数据验证失败 (例如，响应一个 POST 请求)。 请检查响应体内详细的错误消息。
     * 429: 请求过多。 由于限速请求被拒绝。
     * 500: 内部服务器错误。 这可能是由于内部程序错误引起的。
     *
     * @param \yii\base\Event $event
     */
    public function beforeSend($event)
    {
        $response = $event->sender;

        $isSuccessful = $response->isSuccessful;
        if ($response->statusCode>=400) {
            //异常处理
            if (true && $exception = Yii::$app->getErrorHandler()->exception) {
                $resd = $this->convertExceptionToArray($exception);

                $response->data = [
                    'name'   => $resd['name'],
                    'message'=> $resd['message'],
                    'code'   => $resd['code'],
                    'status' => $response->statusCode
                ];
                if (YII_ENV_DEV) {
                    $response->data = $resd;
                    $response->data['code'] = $response->statusCode;
                    $response->data['status'] = $response->statusCode;
                }
            }
            //Model出错了
            if ($response->statusCode==422) {
                $messages=[];
                foreach ($response->data as $v) {
                    $messages[] = $v['message'];
                }
                //请求错误时数据为  {"success":false,"data":{"name":"Not Found","message":"页面未找到。","code":0,"status":404}}
                $response->data = [
                    'name'   => 'valide error',
                    'message'=> implode("  ", $messages),
                    'status' => $response->statusCode,
                    'info'   =>$response->data
                ];
            }else if ($response->statusCode==422) {
                $response->data = [
                    'name'=> 'valide error',
                    'message'=> '无权访问',
                    'info'=>$response->data
                ];
            }
            $response->statusCode = 200;
        }
        elseif ($response->statusCode>=300) {
            $response->data = $this->convertExceptionToArray(new ForbiddenHttpException(Yii::t('yii', 'Login Required')));
            $response->data['code'] = $response->statusCode;
            $response->statusCode = 200;
        }
        //请求正确时数据为  {"success":true,"data":{...}}
        /*$response->data = [
            'success' => $isSuccessful,
            'data' => $response->data,
        ];*/

        $response->format = Response::FORMAT_JSON;
        Yii::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*');
        Yii::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Credentials', 'true');
        //jsonp 格式输出
        if (isset($_GET['callback'])) {
            $response->format = Response::FORMAT_JSONP;
            $response->data = [
                'callback' => $_GET['callback'],
                'data'=>$response->data,
            ];
        }
    }

    /**
     * 将异常转换为array输出
     *
     * @param $exception
     * @return array
     */
    protected function convertExceptionToArray($exception)
    {
        if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
            $exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
        }
        $array = [
            // 'name' => ($exception instanceof \Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception',
            'name' => ($exception instanceof \Exception || $exception instanceof ErrorException) ? $exception->getMessage() : 'Exception',
            'message' => $exception->getMessage(),
            'code' => $exception->getCode(),
        ];
        if ($exception instanceof HttpException) {
            $array['status'] = $exception->statusCode;
        }
        if (YII_DEBUG) {
            $array['type'] = get_class($exception);
            if (!$exception instanceof UserException) {
                $array['file'] = $exception->getFile();
                $array['line'] = $exception->getLine();
                $array['stack-trace'] = explode("\n", $exception->getTraceAsString());
                if ($exception instanceof \yii\db\Exception) {
                    $array['error-info'] = $exception->errorInfo;
                }
            }
        }
        if (($prev = $exception->getPrevious()) !== null) {
            $array['previous'] = $this->convertExceptionToArray($prev);
        }
        return $array;
    }

    /**
     * 操作成功
     * @param array $data
     * @param int $code
     * @param string $msg
     * @return array
     */
    public function success( $data=[] , $code=0 , $msg='success' )
    {
        return [
            'code' => $code ? $code : self::ACTION_SUCCESS_CODE ,
            'message' => $msg ,
            'data' => $data ,
        ];
    }

    /**
     * 操作失败
     *
     * @param string $msg
     * @param array  $data
     * @param int    $count
     *
     * @return array
     */
    public function fail( $msg = '操作失败' , $data = [] , $code=1 )
    {
        return [
            'code' => $code ? $code : self::ACTION_FAIL_CODE ,
            'msg'  => $msg ,
            'data' => $data ,
        ];
    }

    /**
     * 获取GET请求数据
     */
    public function GetData() {
        if (Yii::$app->request->isGet) {
            return Yii::$app->request->get();
        }
        return [];
    }

    /**
     * 获取POST数据
     * @return array|mixed|null
     */
    public function PostData() {
        if (Yii::$app->request->isPost) {
            return Yii::$app->request->post();
        }
        return [];
    }

    /**
     * 获取PUT数据
     * @return array|mixed|null
     */
    public function PutData() {
        if (Yii::$app->request->isPut) {
            return Yii::$app->request->post();
        }
        return [];
    }
}
